async function checkToken(backend_domain_name, token) {
    const formData = new URLSearchParams();
    formData.append('token', token);

    try {
        const response = await fetch(backend_domain_name + '/personal_center/check_token', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData
        });

        const data = await response.json();
        if (data.TF === 'T') {
            return data.username;
        } else {
            return null;
        }
    } catch (error) {
        console.error('请求错误:', error);
        return null;
    }
}

async function changeUsername(backend_domain_name, token, new_username, password){
    if(! new_username){
        return {'TF': 'F', 'message': '用户名不能为空'}
    }
    if (new_username.length < 1 || new_username.length > 32){
        return {'TF': 'F', 'message': '用户名长度必须在 1 到 32 之间'}
    }
    const formData = new URLSearchParams();
    formData.append('token', token);
    formData.append('new_username', new_username);
    formData.append('password', password);

    try {
        const response = await fetch(backend_domain_name + '/personal_center/change_username', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData
        });

        const data = await response.json();

        if (data.TF === 'T') {
            return {
                'TF': 'T', 
                'message': data.message + ' ' + '登录剩余有效时间更新为 24 小时', 
                'token': data.token
            }
        } else {
            return {'TF': 'F', 'message': data.message}
        }
    } catch (error) {
        console.error('请求错误:', error);
        return {'TF': 'F', 'message': '请求出错，请稍后再试'}
    }
}

async function changePassword(backend_domain_name, token, old_password, new_password){
    if(! old_password){
        return {'TF': 'F', 'message': '旧密码不能为空'}
    }
    if (! new_password){
        return {'TF': 'F','message': '新密码不能为空'}
    }
    if (old_password == new_password){
        return {'TF': 'F','message': '新旧密码不能相同'}
    }
    if (new_password.length < 1 || new_password.length > 32){
        return {'TF': 'F', 'message': '密码长度必须在 1 到 32 之间'}
    }
    const formData = new URLSearchParams();
    formData.append('token', token);
    formData.append('old_password', old_password);
    formData.append('new_password', new_password);

    try {
        const response = await fetch(backend_domain_name + '/personal_center/change_password', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData
        });

        const data = await response.json();

        if (data.TF === 'T') {
            return {
                'TF': 'T', 
                'message': data.message + ' ' + '登录剩余有效时间更新为 24 小时', 
                'token': data.token
            }
        } else {
            return {'TF': 'F', 'message': data.message}
        }
    } catch (error) {
        console.error('请求错误:', error);
        return {'TF': 'F', 'message': '请求出错，请稍后再试'}
    }
}
